#!/bin/bash
[% c('var/setarch') %]
[% c("var/set_default_env") -%]
distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/dist
tar -C /var/tmp/dist -xf [% c('input_files_by_name/cmake') %]
export PATH="/var/tmp/dist/cmake/bin:$PATH"
tar -C /var/tmp/dist -xf [% c('input_files_by_name/prev_rust') %]
cd /var/tmp/dist/rust-[% c('var/prev_version') %]-[% c('arch') %]-unknown-linux-gnu
./install.sh --prefix=$distdir-rust-old
export PATH="$distdir-rust-old/bin:$PATH"

[% IF ! c("var/windows") %]
  [% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
[% END -%]

[% IF c("var/osx") %]
  # We need to clear `CC` and `LDFLAGS` as they are used for the host platform
  # (i.e. Linux).
  unset CC
  unset LDFLAGS
  # Target 10.6 as our toolchain does. Without this explicit declaration Bad
  # Things will happen, as a lot of dependent code then assumes that the
  # official macOS target, x86_64-apple-darwin, essentially means 10.4.
  export MACOSX_DEPLOYMENT_TARGET=10.6
  # The Rust target for macOS is x86_64-apple-darwin, yet our toolchain is built
  # for x86_64-apple-darwin10. We can't mix those targets as clang gets confused
  # that way. Changing the Rust target to x86_64-apple-darwin10 would require a
  # fair amount of patching, thus we create symlinks to prvoide Rust with the
  # necessary tools while using our toolchain underneath, targeting 10.6.
  cd $cctoolsdir
  for f in `ls x86_64-apple-darwin10-*`; do
    ln -s $f ${f//x86_64-apple-darwin10/x86_64-apple-darwin}
  done
  cd ..
  ln -s x86_64-apple-darwin10 x86_64-apple-darwin
  mkdir -p $distdir/helper

  # We need to adapt our CFLAGS and make sure our flags are passed down to all
  # dependencies. Using `CFLAGS_x86_apple-darwin` did not do the trick, so resort
  # to a wrapper script.
  cat > $distdir/helper/x86_64-apple-darwin-clang << 'EOF'
#!/bin/sh
BASEDIR=/var/tmp/dist/macosx-toolchain
$BASEDIR/cctools/bin/x86_64-apple-darwin-clang -target x86_64-apple-darwin -mlinker-version=136 -B $BASEDIR/cctools/bin -isysroot $BASEDIR/SDK/ -Wl,-syslibroot,$BASEDIR/SDK/ -Wl,-dead_strip -Wl,-pie "$@"
EOF

  chmod +x $distdir/helper/x86_64-apple-darwin-clang
  export PATH=$distdir/helper:$PATH
[% END %]

cd $rootdir
mkdir /var/tmp/build
tar -C /var/tmp/build -xf  [% c('input_files_by_name/rust') %]
cd /var/tmp/build/rustc-[% c('version') %]-src

[% IF c("var/linux") %]
  # binaryen hardcodes /usr/bin/cc and /usr/bin/c++ as the C and C++ compiler.
  # But that is too old on Debian Wheezy which is why we need to patch it, so
  # we can use our own GCC instead.
  patch -p1 < $rootdir/binaryen.patch
[% END %]

mkdir build
cd build
../configure --prefix=$distdir [% c("var/configure_opt") %]
make -j[% c("buildconf/num_procs") %]
make install
cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
        }) %]
